package com.zhdl.common.ytools;

import cn.hutool.core.util.HexUtil;
import com.zhdl.common.ytools.IntToHexUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HexToBitTools {


    /**
     * 修改某个寄存器中的某一位
     *
     * @param hex
     * @param bit
     * @param value
     * @return
     */
    public static String writeValueByBit(String hex, int bit, int value){

        /* 十六进制转换为十进制 */
        int dec = Integer.parseInt(hex, 16);
        /* 十进制转换为二进制字节 */
        String hexString = Integer.toBinaryString(dec);

        if (hexString.length() < 16){
            StringBuilder zero = new StringBuilder();
            int len = 16 - hexString.length();
            zero.append("0".repeat(len));
            hexString = zero + hexString;
        }

        /* 前八字节 */
        String before = "00000000";
        /* 后八字节 */
        String after = "00000000";

        before = hexString.substring(0, 8);
        after = hexString.substring(8, 16);

        StringBuffer stringBufferBefore = new StringBuffer(before);
        StringBuffer stringBufferAfter = new StringBuffer(after);
        int position = -1;

        if (bit < 8){
            position = 8 - bit;
        } else if (bit < 16){
            position = 24 - bit;
        }
        StringBuffer all =  new StringBuffer(before+after);

        if (value == 0) {
            all.replace(position-1, position, "0");
        } else {
            all.replace(position-1, position, "1");
        }

//        String cmd = stringBufferBefore.toString() + stringBufferAfter.toString();
        String cmd = all.toString();

        log.info("二进制--->"+cmd);

        int i = Integer.parseInt(cmd, 2);
        String hexValue = IntToHexUtil.shortToHex(i);
        log.info("十六进制--->"+hexValue);
        return hexValue;
    }

    /**
     * 多个寄存器的值中找到某一个寄存器的某一位
     *
     * @param hexValue  全量十六进制寄存器值
     * @param decAddress    十进制的寄存器地址
     * @param bit   位
     * @return  位值
     */
    public static String getBitByHexValue(String hexValue,int decAddress, int bit){
        String hexAddress;
        if (decAddress % 2 == 0){
            hexAddress = IntToHexUtil.shortToHex(decAddress);
        } else {
            hexAddress = IntToHexUtil.shortToHex(decAddress - 1);
        }
        String hexByAddressValue = readHexValueByAddress(hexValue, hexAddress);
        String bits = readBitValueByHex(hexByAddressValue);
        return getBitByBits(bits, bit);
    }

    public static String getBitByBits(String bits, int bit){
        return bits.substring(bit,bit+1);
    }

    /**
     * 将byte转换为一个长度为8的byte数组，数组每个值代表bit
     */
    public static Byte[] getBitArray(byte b) {
        Byte[] array = new Byte[8];
        for (int i = 7; i >= 0; i--) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }

    /**
     * 将某个寄存器的值 转为 bit位
     *
     * @param hexString 十六进制
     * @return string类型的bit
     */
    public static String readBitValueByHex(String hexString){
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < hexString.length(); i++) {
            if (i % 4 == 0){
                int dec = Integer.parseInt(hexString.substring(i,i+4), 16);
                String binaryString = Integer.toBinaryString(dec);
                String number = "";
                if (binaryString.length() < 16){
                    for (int i1 = 0; i1 < 16 - binaryString.length(); i1++) {
                        number += "0";
                    }
                    binaryString = number + binaryString;
                }
                temp.append(binaryString);
            }
        }
        String reverse = new StringBuffer(temp.substring(0,8)).reverse().toString()+new StringBuffer(temp.substring(8,16)).reverse().toString();
        log.info("readBitValueByHex>>>>>>>>>>>>>>>>>>>>>>>"+reverse);
        return reverse;
    }

    public static String readHexValueByDecAddress(String hexData, int decAddress){
        StringBuilder temp = new StringBuilder(IntToHexUtil.shortToHex(decAddress));
        if (temp.length() < 4){
            for (int i = 0; i < 4 - temp.length(); i++) {
                temp.insert(0, "0");
            }
        }
        String hexAddress = temp.toString();
        return readHexValueByAddress(hexData,hexAddress);
    }

    public static String readHexValueByAddress(String hexData,String hexAddress){
        int decAddress = Integer.parseInt(hexAddress, 16);
        int startAddr = (decAddress * 4);
        int endAddr = (startAddr + 4);
        String substring = hexData.substring(startAddr, endAddr);
        log.info("readHexValueByAddress --> " + substring);
        return substring;
    }


}
